package cn.yunyichina.provider.register.listener;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.register.service.impl.PayServiceImpl;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import cn.yunyichina.utils.convert.XmlUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.listener.MessageListener;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class RegWechatPayMessageListener implements MessageListener<Integer, String> {

    private Logger logger = LoggerFactory.getLogger(RegWechatPayMessageListener.class);
    @Autowired
    private static PayServiceImpl payService;

    public void onMessage(ConsumerRecord<Integer, String> integerStringConsumerRecord) {
//        String xmlString = "<xml><appid>wx2421b1c4370ec43b</appid><attach>支付测试</attach><bank_ty pe>CFT</bank_type><fee_type>CNY</fee_type><is_subscribe>Y</is_subscribe><mch_id>10000100</mch_id><nonce_str>5d2b6c2a8db53831f7eda20af46e531c</nonce_str><openid>oUpF8uMEb4qRXf22hE3X68TekukE</openid><out_trade_no>1409811653</out_trade_no><result_code>SUCCESS</result_code><return_code>SUCCESS</return_code><sign>B552ED6B279343CB493C5DD0D78AB241</sign><sub_mch_id>10000100</sub_mch_id><time_end>20140903131540</time_end><total_fee>1</total_fee><trade_type>JSAPI</trade_type><transaction_id>1004400740201409030005092168</transaction_id></xml>";
        String xmlString = integerStringConsumerRecord.value();
        logger.info("RegWechatPayMessageListener.onMessage.xmlString -> {}", xmlString);

        try {
            Map<String, String> doc = XmlUtils.xmlToMap(xmlString);
            String orderNo = doc.get("out_trade_no");
            String openId = doc.get("openid");
            String appId = doc.get("appid");
//            Float payAmount = Float.parseFloat((String) doc.get("total_fee"));
            BigDecimal payAmount = new BigDecimal(doc.get("total_fee"));
            String transactionId = doc.get("transaction_id");

            String payMode = "12";
            String attach = doc.get("attach");
            if(StringUtils.isNotBlank(attach)) {
                JSONObject attachObj = JSON.parseObject(attach);
                payMode = (String) attachObj.get("pay_mode");
            }

            Map<String, Object> dataMap = new HashMap<String, Object>();

            dataMap.put("yyt_order_no", orderNo);
            dataMap.put("pay_time", DateUtils.dateToString(new Date()));
            dataMap.put("open_id", openId);
            dataMap.put("payee_app_id", appId);
            dataMap.put("pay_mode", payMode);
            dataMap.put("pay_amount", payAmount.intValue());
            dataMap.put("payment_deal_id", transactionId);

            if(payService == null){
                payService = SpringContextHandler.getBean(PayServiceImpl.class);
            }
            Response response = payService.ackPayRegRecord( JsonUtils.toJsonString(dataMap),"");
            logger.info("onMessage 挂号写单 response:{}", JSON.toJSONString(response));
//
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("RegWechatPayMessageListener,解析回调出错", e);
        }
        logger.info("onMessage end.");
    }

}
